package com.rabbit.util;

import com.xxl.job.core.context.XxlJobHelper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <h1>Job中打印日志工具类</h1>
 *
 * @author zjw
 * @date 2021/4/1 20:49
 */
@Slf4j
public class JobLog {

    /**
     * 各种打印日志方法
     *
     * @param msg 内容
     */
    public static void info(String msg) {
        log.info(msg);
        XxlJobHelper.log(msg);
    }

    public static void info(String msg, Object... arguments) {
        log.info(msg, arguments);
        XxlJobHelper.log(msg, arguments);
    }

    public static void error(String msg, Object... arguments) {
        log.error(msg, arguments);
        if (arguments[arguments.length - 1] instanceof Exception) {
            Exception e = (Exception) arguments[arguments.length - 1];
            XxlJobHelper.log(msg, arguments);
            XxlJobHelper.log(e.getCause());
        }
    }

    /**
     * 最终结果异常，需要触发邮件时调用
     *
     * @param msg 邮件内容
     * @param arguments 参数
     */
    public static void handleFail(String msg, Object... arguments) {
        log.error(msg, arguments);

        if(arguments == null || arguments.length == 0){
            XxlJobHelper.handleFail(msg);
            return;
        }

        if (arguments[arguments.length - 1] instanceof Exception) {
            Exception e = (Exception) arguments[arguments.length - 1];
            XxlJobHelper.log(msg, arguments);
            XxlJobHelper.log(e.getCause());
        }
        XxlJobHelper.handleFail(msg);
    }

}
